#include "apue.h"
#include <ctype.h>
#include <fcntl.h>

#define BSZ 4096

unsigned char buf[BSZ];

unsigned char translate(unsigned char c) {
    if (isalpha(c)) {
        if (c >= 'n') 
            c-= 13;
        else if (c >= 'a')
            c+= 13;
        else if (c >= 'N')
            c-= 13;
        else if (c >= 'A')
            c+= 13;
    }
    return c;
}

int main(int argc, char ** argv) {
    int ifd, ofd, i, n, nw;

    if (argc != 3) 
        err_quit("Usage: rot13 infile outfile");
    if ((ifd= open(argv[1], O_RDONLY)) < 0)
        err_sys("cant open %s", argv[1]);
    if ((ofd= open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, FILE_MODE)) < 0)
        err_sys("cant create %s", argv[2]);

    while ((n= read(ifd, buf, BSZ)) > 0) {
        for (i=0; i<n; ++i) {
            buf[i]= translate(buf[i]);
        }
        if ((nw= write(ofd, buf, n)) != n) {
            if (nw < 0)
                err_sys("write error");
            else 
                err_quit("short write (%d/%d)", nw, n);
        }

    }
    if (n < 0)
        err_sys("read error");

    fsync(ofd);
    exit(0);
}


